
##xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx pytorch
##torch cmake folder
# 查找Torch安装目录的CMake代码
# 支持Windows和Linux平台
# 初始化Torch安装目录变量
set(my_TORCH_PKG_DIR "")
# 根据不同平台设置候选路径
if(WIN32)
## Windows平台候选路径
    set(torch_candidate_paths
        "C:/cpplibs/libtorch"
        "$ENV{LOCALAPPDATA}/Programs/Python/Python312/Lib/site-packages/torch"
        # 增加Python常见版本路径
        "$ENV{LOCALAPPDATA}/Programs/Python/Python311/Lib/site-packages/torch"
    )
elseif(UNIX AND NOT APPLE)
## Linux平台候选路径
    set(torch_candidate_paths
        "/usr/local/lib/python3.12/dist-packages/torch"
        "/usr/lib/python3/dist-packages/torch"
        "$ENV{HOME}/.local/lib/python3.12/site-packages/torch"
        "/usr/local/lib/python3.11/dist-packages/torch"
        "/usr/lib/python3.11/dist-packages/torch"
        "$ENV{HOME}/.local/lib/python3.11/site-packages/torch"
    )
endif()
## 检查每个候选路径
foreach(path IN LISTS torch_candidate_paths)
    if(EXISTS "${path}")
        set(my_TORCH_PKG_DIR "${path}")
        message(STATUS "Torch package directory found: ${my_TORCH_PKG_DIR}")
        break()
    endif()
endforeach()

## 如果未找到，给出提示
if(NOT my_TORCH_PKG_DIR)
    message(FATAL_ERROR "Torch CMake directory missing, please ensure PyTorch installed")
endif()

message(STATUS "[torch] CUDA_nvrtc_LIBRARY => ${CUDA_nvrtc_LIBRARY}")
# set(TORCH_CUDA_ARCH_LIST "8.6")
list(PREPEND CMAKE_PREFIX_PATH "${my_TORCH_PKG_DIR}/share/cmake")


##======== find pytorch
find_package(Torch REQUIRED)
message(STATUS "[torch]TORCH_INCLUDE_DIRS => ${TORCH_INCLUDE_DIRS}")
message(STATUS "[torch]TORCH_LIBRARIES => ${TORCH_LIBRARIES}")
message(STATUS "[torch]TORCH_CXX_FLAGS => ${TORCH_CXX_FLAGS}")
message(STATUS "[torch]Caffe2_MAIN_LIBS => ${Caffe2_MAIN_LIBS}")
message(STATUS "[torch]CAFFE2_INCLUDE_DIRS => ${CAFFE2_INCLUDE_DIRS}")
message(STATUS "[torch]ATEN_INCLUDE_DIR => ${ATEN_INCLUDE_DIR}")
message(STATUS "[torch]ATEN_LIBRARIES => ${ATEN_LIBRARIES}")

get_target_property(tmpl torch INTERFACE_LINK_LIBRARIES)
message(STATUS "[torch] torch.INTERFACE_LINK_LIBRARIES ->${tmpl}")

get_target_property(tmpl torch_cpu_library INTERFACE_LINK_LIBRARIES)
message(STATUS "[torch] torch_cpu_library.INTERFACE_LINK_LIBRARIES ->${tmpl}")

get_target_property(tmpl torch_cuda_library INTERFACE_LINK_LIBRARIES)
message(STATUS "[torch] torch_cuda_library.INTERFACE_LINK_LIBRARIES ->${tmpl}")